package rc.database.DokTipas;

/**
 * Created by IntelliJ IDEA.
 * User: Slejus
 * Date: 13.6.11
 * Time: 13.16
 * To change this template use File | Settings | File Templates.
 */

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import rc.database.connection.DBOperations;

import java.io.IOException;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DokTipasWebScript extends AbstractWebScript {

    private final String COL_ONE =  "DOKU_TIPAS";
    private final String COL_TWO =  "DOKU_PAV_I";
    private final String TITLE   =  "antraste";
    private final String ID      =  "id";

    public void execute(WebScriptRequest wsr, WebScriptResponse wsr1) throws IOException{
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/extension/connection-context.xml");
            DBOperations db = (DBOperations)context.getBean("dbOperations"); // DB operations object
            Class.forName(db.getJdbcDriver());
            conn = DriverManager.getConnection(db.getJdbcUrl(), db.getUsername(), db.getPassword());
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(db.getDocTypesQuery());

            JSONObject obj = new JSONObject();
            db.appendDocTypesJSONData(obj, rs, ID, TITLE, COL_ONE, COL_TWO);
            String jsonString = obj.toString();
            wsr1.setContentType("application/json; charset=utf-8");
            wsr1.getWriter().write(jsonString);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DokTipasWebScript.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DokTipasWebScript.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JSONException ex) {
            Logger.getLogger(DokTipasWebScript.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {Logger.getLogger(DokTipasWebScript.class.getName()).log(Level.SEVERE, null, e);}
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {Logger.getLogger(DokTipasWebScript.class.getName()).log(Level.SEVERE, null, e);}
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {Logger.getLogger(DokTipasWebScript.class.getName()).log(Level.SEVERE, null, e);}
            }
        }
    }
}
